// pages/movies/detail/detail.js
import { httpBlock } from '../../../utils/util'

let app = getApp();
const globalData = app.globalData;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    otherInfo: {
      rating: {},
      trailers: {},
      interests: {},
      recommendations: {},
      reviews: {},
      photos: {}
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let _this = this
    const id = options.id
    const type = options.type
    wx.request({
      url: `${globalData.g_requestApi}${type}/${id}?apiKey=054022eaeae0b00e0fc068c0c0a2102a`,
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        const data = res.data
        let title = ''
        if (data.title.length >= 8) {
          title = data.title
        } else if (data.year) {
          title = `${data.title} (${data.year})`
        } else {
          title = data.title
        }
        let summary = ''
        let recommend = ''
        let review = ''
        if (type === 'movie') {
          recommend = '喜欢该部电影的人也喜欢'
          review = '影评'
          summary = `${data.genres.join(' ')} / ${data.countries.join(' ')} / ${data.release_date}上映 / 片长${data.durations.join(' ')}`
        } else if (type === 'book') {
          recommend = '喜欢该本书的人也喜欢'
          review = '书评'
          summary = `${data.author} / ${data.press} / 出版时间: ${data.pubdate}`
        } else if (type === 'music') {
          recommend = '喜欢该张唱片的人也喜欢'
          review = '乐评'
          summary = `${data.genres.join(' ')} / ${data.card_subtitle.split('/')[0]} / ${data.pubdate}`
        } else {
          recommend = '喜欢该电影的人也喜欢'
          review = '影评'
          summary = `${data.genres.join(' ')} / ${data.countries.join(' ')} / ${data.release_date}首映 / ${data.episodes_count}集 / 单集片长${data.durations}`
        }
        _this.setData({
          itemDetail: data,
          summary: summary,
          recommend: recommend,
          review: review,
          title: title,
          pageBackgroundColor: `#${data.body_bg_color}`
        })
        wx.setNavigationBarTitle({
          title: data.title,
        })
      }
    })
    const rating = `${globalData.g_requestApi}${type}/${id}/rating?apiKey=054022eaeae0b00e0fc068c0c0a2102a`
    const trailers = `${globalData.g_requestApi}${type}/${id}/trailers?apiKey=054022eaeae0b00e0fc068c0c0a2102a`
    const interests = `${globalData.g_requestApi}${type}/${id}/interests?start=0&count=4&apiKey=054022eaeae0b00e0fc068c0c0a2102a`
    const recommendations = `${globalData.g_requestApi}${type}/${id}/recommendations?apiKey=054022eaeae0b00e0fc068c0c0a2102a`
    const reviews = `${globalData.g_requestApi}${type}/${id}/reviews?start=0&count=20&apiKey=054022eaeae0b00e0fc068c0c0a2102a`
    const photos = `${globalData.g_requestApi}${type}/${id}/photos?start=0&count=20&apiKey=054022eaeae0b00e0fc068c0c0a2102a`

    if (type === 'movie' || type === 'tv') {
      httpBlock(trailers, 'trailers', this.processData)
      httpBlock(photos, 'photos', this.processData)
    }
    httpBlock(rating, 'rating', this.processData)
    httpBlock(interests, 'interests', this.processData)
    httpBlock(recommendations, 'recommendations', this.processData)
    httpBlock(reviews, 'reviews', this.processData)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log(this.data)
  },

  //跳转到详情
  onTapDetail: function (event) {
    console.log(event)
    const id = event.target.dataset.id
    const type = event.target.dataset.type
    wx.navigateTo({
      url: `../detail/detail?id=${id}&&type=${type}`
    })
  },
  
  //回调赋值
  processData: function (key, data) {
    this.data.otherInfo[key] = data
    this.setData({
      otherInfo: this.data.otherInfo
    })
  }
})